﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MailSortSettingServiceTests.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the MailSortSettingServiceTests type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceTests
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using DocProcessingRepository.Interfaces;
  using Entities;
  using Exceptions;
  using FluentAssertions;
  using Moq;
  using NUnit.Framework;
  using ServiceInterfaces;
  using Services;

  [Category("Unit")]
  [TestFixture]
  public class MailSortSettingServiceTests
  {
    private IMailSortSettingService mailSortSettingService;
    private Mock<IMailSortSettingRepository> mailSortSettingRepositoryMock;

    [SetUp]
    public void SetUp()
    {
      this.mailSortSettingRepositoryMock = new Mock<IMailSortSettingRepository>();
      this.mailSortSettingService = new MailSortSettingService(this.mailSortSettingRepositoryMock.Object);
    }

    [Test]
    public void GivenValidData_WhenIGetTheMailSortSetting_TheyAreRetrievedFromTheDatabase()
    {
      var mailSortsToReturn = new List<MailsortSetting> { new MailsortSetting { MailsortSettingID = 1 } };

      this.mailSortSettingRepositoryMock.Setup(m => m.GetMailsortSettings())
        .Returns(mailSortsToReturn);

      var result = this.mailSortSettingService.GetMailsortSettings();
      
      var resultAsObj = (Object)result;
      resultAsObj.Should().BeSameAs(mailSortsToReturn);
    }

    [Test]
    public void GivenValidData_WhenIGetTheMailSortSetting_AndTheDatabaseIsUnavailable_ADocProcessingExceptionIsRaised()
    {
      this.mailSortSettingRepositoryMock.Setup(m => m.GetMailsortSettings()).Throws<Exception>();

      Action act = () => this.mailSortSettingService.GetMailsortSettings();

      act.ShouldThrow<DocProcessingException>();
    }

    [Test]
    public void GivenValidData_WhenIAddAMailSortSetting_ItIsAddedToTheDatabase()
    {
      var createdMailSortSetting = new MailsortSetting { MailsortSettingID = 1 };

      this.mailSortSettingRepositoryMock.Setup(
        m =>
        m.AddMailSortSetting(
          It.IsAny<String>(),
          It.IsAny<String>(),
          It.IsAny<String>(),
          It.IsAny<String>(),
          It.IsAny<String>(),
          It.IsAny<Boolean>(),
          It.IsAny<Int32>()))
      .Returns(createdMailSortSetting);

      MailsortSetting result = this.mailSortSettingService.AddMailSortSetting(
        "testDepot",
        "testMailingHouse",
        "testClientCode",
        "testJobReference",
        "testMailsortService",
        true,
        10);

      this.mailSortSettingRepositoryMock.Verify(
        m =>
        m.AddMailSortSetting(
          "testDepot",
          "testMailingHouse",
          "testClientCode",
          "testJobReference",
          "testMailsortService",
          true,
          10));

      var resultObj = (Object)result;
      resultObj.Should().BeSameAs(createdMailSortSetting);
    }

    [Test]
    public void GivenValidData_WhenIAddAMailSortSetting_AndTheDatabaseIsUnavailable_ADocProcessingExceptionIsRaised()
    {
      this.mailSortSettingRepositoryMock.Setup(
        m =>
        m.AddMailSortSetting(
          It.IsAny<String>(),
          It.IsAny<String>(),
          It.IsAny<String>(),
          It.IsAny<String>(),
          It.IsAny<String>(),
          It.IsAny<Boolean>(),
          It.IsAny<Int32>()))
      .Throws<Exception>();

      Action act = () => this.mailSortSettingService.AddMailSortSetting(
        "testDepot",
        "testMailingHouse",
        "testClientCode",
        "testJobReference",
        "testMailsortService",
        true,
        10);

      act.ShouldThrow<DocProcessingException>();
    }
  }
}
